package www.qiyue.transfer;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InterfaceHttpUtil {
    private static ObjectMapper mapper = new ObjectMapper();


    public static void main(String[] args) throws IOException {

        String token = getToken();
        System.out.println("token = " + token);
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("type", 1);
        paramMap.put("page", 1);
        paramMap.put("pageSize", 200);
        paramMap.put("token", token);
        String json = sendPost("http://zc.shanghai-garment.com/restful/sasac/getData.htm", paramMap);
        Map<String, Object> mapjson = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
        });
        Boolean success = (Boolean) mapjson.get("success");
        String message = mapjson.get("message") + "";
        Integer page = (Integer) mapjson.get("page");
        Integer pageSize = (Integer) mapjson.get("pageSize");
        Integer total = (Integer) mapjson.get("total");
        List<Map<String, String>> dataList = (List<Map<String, String>>) mapjson.get("data");
        for (Map<String, String> data : dataList) {
            System.out.println(data);
        }

    }


    public static String sendPost(String url, HashMap<String, Object> map) {
        String post = HttpUtil.post(url, map);
        return post;
    }

    private static String getToken() {
        String token = "{}DATA{}TO{}{}SH{}{}SASAC{}";
        Calendar calendar = Calendar.getInstance();
        /**
         * 获取 年 ，月 ，日
         */
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH) + 1;
        Integer date = calendar.get(Calendar.DATE);
        String format = StrUtil.format(token, String.format("%02d", month + date),
                year, RandomUtil.randomNumbers(2), String.format("%02d", month), String.format("%02d", date),
                RandomUtil.randomNumbers(2), RandomUtil.randomNumbers(2));
        return Base64.encode(Base64.encode(format));
    }
}
